package com.yy.micro.magic.data.service.code.impl;

import com.yy.micro.magic.data.domain.CodeMapVo;
import com.yy.micro.magic.data.entity.CodeMapEntity;
import com.yy.micro.magic.data.repository.CodeMapRepository;
import com.yy.micro.magic.data.service.code.MagicCodeMapService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class MagicCodeMapServiceImpl implements MagicCodeMapService {
    @Autowired
    private CodeMapRepository repository;

    @Override
    public CodeMapVo apply(final CodeMapVo vo) {
        CodeMapEntity entity=repository.save(vo.to());
        return CodeMapVo.create().from(entity);
    }

    @Override
    public CodeMapVo load(final Long id) {
        Optional<CodeMapEntity> entity = repository.findById(id);
        if (entity.isPresent()){
            return CodeMapVo.create().from(entity.get());
        }
        return null;
    }

    @Override
    public CodeMapVo findByName(final String name) {
        CodeMapEntity entity = repository.findByCodeNameAndDisable(name,false);
        if (null != entity){
            return CodeMapVo.create().from(entity);
        }
        return null;
    }
}
